﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using Obvious.Framework.Logging.Enums;

namespace Obvious.Framework.Logging.ConfigHandlers
{
    public class ObviousLoggerSection : ConfigurationSection
    {
        [ConfigurationProperty(Constants.C_CONFIG_ATTRIBUTE_DEFAULT_PROVIDER, IsRequired = true)]
        public string DefaultProvider
        {
            get
            {
                return (string)this[Constants.C_CONFIG_ATTRIBUTE_DEFAULT_PROVIDER];
            }
            set
            {
                this[Constants.C_CONFIG_ATTRIBUTE_DEFAULT_PROVIDER] = value;
            }
        }
        [ConfigurationProperty(Constants.C_CONFIG_COLLECTION_LOGGER_PROVIDERS)]
        public LoggerProviderElementCollection LoggerProviders
        {
            get
            {
                return (LoggerProviderElementCollection)this[Constants.C_CONFIG_COLLECTION_LOGGER_PROVIDERS];
            }
            set
            {
                this[Constants.C_CONFIG_COLLECTION_LOGGER_PROVIDERS] = value;
            }
        }
    }

    public class LoggerProviderElementCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new LoggerProviderElement();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((LoggerProviderElement)element).Name;
        }
        public LoggerProviderElement this[int index]
        {
            get
            {
                return (LoggerProviderElement)BaseGet(index);
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }
        new public LoggerProviderElement this[string Name]
        {
            get
            {
                return (LoggerProviderElement)BaseGet(Name);
            }
        }
    }

    public class LoggerProviderElementSettingsCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new LoggerProviderSettingsElement();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((LoggerProviderSettingsElement)element).Key;
        }
        public LoggerProviderSettingsElement this[int index]
        {
            get
            {
                return (LoggerProviderSettingsElement)BaseGet(index);
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }
        new public LoggerProviderSettingsElement this[string Name]
        {
            get
            {
                return (LoggerProviderSettingsElement)BaseGet(Name);
            }
        }
    }

    public class LoggerProviderSettingsElement : ConfigurationElement
    {
        [ConfigurationProperty(Constants.C_CONFIG_ATTRIBUTE_KEY, IsRequired = true)]
        public string Key
        {
            get
            {
                return (string)this[Constants.C_CONFIG_ATTRIBUTE_KEY];
            }
            set
            {
                this[Constants.C_CONFIG_ATTRIBUTE_KEY] = value;
            }
        }
        [ConfigurationProperty(Constants.C_CONFIG_ATTRIBUTE_VALUE, IsRequired = true)]
        public string Value
        {
            get
            {
                return (string)this[Constants.C_CONFIG_ATTRIBUTE_VALUE];
            }
            set
            {
                this[Constants.C_CONFIG_ATTRIBUTE_VALUE] = value;
            }
        }
    }

    public class LoggerProviderElement : ConfigurationElement
    {
        [ConfigurationProperty(Constants.C_CONFIG_COLLECTION_SETTINGS)]
        public LoggerProviderElementSettingsCollection Settings
        {
            get
            {
                return (LoggerProviderElementSettingsCollection)this[Constants.C_CONFIG_COLLECTION_SETTINGS];
            }
            set
            {
                this[Constants.C_CONFIG_COLLECTION_SETTINGS] = value;
            }
        }
        [ConfigurationProperty(Constants.C_CONFIG_ATTRIBUTE_TYPE, IsRequired = true)]
        public LoggerType Type
        {
            get
            {
                return (LoggerType)this[Constants.C_CONFIG_ATTRIBUTE_TYPE];
            }
            set
            {
                this[Constants.C_CONFIG_ATTRIBUTE_TYPE] = value;
            }
        }
        [ConfigurationProperty(Constants.C_CONFIG_ATTRIBUTE_CUSTOM_TYPE, DefaultValue = "", IsRequired = false)]
        public string CustomType
        {
            get
            {
                return (string)this[Constants.C_CONFIG_ATTRIBUTE_CUSTOM_TYPE];
            }
            set
            {
                this[Constants.C_CONFIG_ATTRIBUTE_CUSTOM_TYPE] = value;
            }
        }
        [ConfigurationProperty(Constants.C_CONFIG_ATTRIBUTE_NAME, IsRequired = true)]
        public string Name
        {
            get
            {
                return (string)this[Constants.C_CONFIG_ATTRIBUTE_NAME];
            }
            set
            {
                this[Constants.C_CONFIG_ATTRIBUTE_NAME] = value;
            }
        }
        [ConfigurationProperty(Constants.C_CONFIG_ATTRIBUTE_DB_SERVER, DefaultValue = ".", IsRequired = false)]
        public string DBLogServer
        {
            get
            {
                return (string)this[Constants.C_CONFIG_ATTRIBUTE_DB_SERVER];
            }
            set
            {
                this[Constants.C_CONFIG_ATTRIBUTE_DB_SERVER] = value;
            }
        }
        [ConfigurationProperty(Constants.C_CONFIG_ATTRIBUTE_DB_LOGCATALOG, DefaultValue = "DB_LOG", IsRequired = false)]
        public string DBLogCatalog
        {
            get
            {
                return (string)this[Constants.C_CONFIG_ATTRIBUTE_DB_LOGCATALOG];
            }
            set
            {
                this[Constants.C_CONFIG_ATTRIBUTE_DB_LOGCATALOG] = value;
            }
        }
        [ConfigurationProperty(Constants.C_CONFIG_ATTRIBUTE_DB_USERNAME, DefaultValue = "", IsRequired = false)]
        public string DBUserName
        {
            get
            {
                return (string)this[Constants.C_CONFIG_ATTRIBUTE_DB_USERNAME];
            }
            set
            {
                this[Constants.C_CONFIG_ATTRIBUTE_DB_USERNAME] = value;
            }
        }
        [ConfigurationProperty(Constants.C_CONFIG_ATTRIBUTE_DB_PWD, DefaultValue = "", IsRequired = false)]
        public string DBPwd
        {
            get
            {
                return (string)this[Constants.C_CONFIG_ATTRIBUTE_DB_PWD];
            }
            set
            {
                this[Constants.C_CONFIG_ATTRIBUTE_DB_PWD] = value;
            }
        }
    }
}
